<template>
  <div>
    <h1>1.forEach</h1>
    <button @click="toEach">提交</button>
    <br>
    <h1>2.map</h1>
    <button @click="toMap">提交</button>
    <br>
    <h1>3.filter</h1>
    <button @click="toFilter">提交</button>
    <br>
    <h1>4.reduce</h1>
    <button @click="toReduce">提交</button>
    <br>
    <h1>5.find</h1>
    <button @click="toFind">提交</button>
    <br>
    <h1>6.findIndex</h1>
    <button @click="toFindIndex">提交</button>
    <br>
    <h1>7.some</h1>
    <button @click="toSome">提交</button>
    <br>
    <h1>8.every</h1>
    <button @click="toEvery">提交</button>
    <br>
  </div>
</template>

<script>
export default {
  data(){
    return{
      arr: [5,6,7,8],
        obj: {
          name: '阿智',
          age: '22',
          sex: 'male'
        },
        dataList: [
          {
            status: 'success',
            msg: '成功信息'
          },
          {
            status: 'error',
            msg: '失败信息'
          },
          {
            status: 'error',
            msg: '失败信息'         
          }      
        ]
     }
  },
  methods:{
    // Each
    toEach(){
      this.arr.forEach((item) => {
      console.log(item)
      })
    },
    //Map 
    toMap(){
      let msgs = this.dataList.map((item) => item.msg)
      let statusList = this.dataList.map((item) => item.status)
      console.log('msgs', msgs, 'statusList', statusList)
    },
    //Filter 
    toFilter(){
      let newList = this.dataList.filter((item) => item.status === 'error')
      console.log(newList)
    },
    //Reduce 
    toReduce(){
      let sum = this.arr.reduce((sum, item) => (sum += item), 0)
      console.log(sum)
    },
    //Find 
    toFind(){
      let str = '信息'
      let target = this.dataList.find((obj) => obj.msg === str)
      console.log('target', target ? target : '无')
    },
    //FindIndex 
    toFindIndex(){
      let str = '信息'
      let targetIndex = this.dataList.findIndex((obj) => obj.msg === str)
      console.log('targetIndex', targetIndex)
    },
    //Some 
    toSome(){
      let flag = this.dataList.some((obj) => obj.status === 'success')
      let flag2 = this.dataList.some((obj) => obj.status === 'error')
      let flag3 = this.dataList.some((obj) => obj.msg === '待定')
      console.log('flag', flag, flag2, flag3)
    },
    //Every 
    toEvery(){
      let flag = this.dataList.every((obj) => obj.status === 'success')
      let flag2 = this.dataList.every((obj) => obj.status === 'error')
      let flag3 = this.dataList.every((obj) => obj.msg.length === 4) 
      console.log('flag', flag, flag2, flag3)
    }
  }
}
</script>

<style>

</style>